<?php
/**
 * 会员日志
 */
namespace App\Http\Controllers;


use App\Service\MemberSubsService;
use App\Validation\DeleteValidation;
use App\Validation\MemberSubsValidation;
use App\Validation\ListValidation;
use App\Service\ConfigsService;
use App\Service\HouseService;

class MemberSubsController extends BaseController
{

	/**
	 * [indexAction 列表]
	 * p:当前页数
	 * page_num:每页数量
	 * @return [type] [description]
	 */
	public function indexAction()
	{
		$this->getUser();
        $this->logger->info(json_encode($this->user));
		$raw = $this->validation(new ListValidation, $this->request->getJsonRawBody());
		$data = MemberSubsService::getList($raw);

		$arr = new \stdClass();
		$arr->name = "subs_bk_pic";
		$pics = ConfigsService::getVal($arr);
		$pics = json_decode(json_encode($pics),TRUE);

		$d = [];
		foreach ($data as $key => $value) {
			$d[$key] = $value;
			$d[$key]->region = explode(",",$value->region)[0];
			$d[$key]->layout = explode(",",$value->layout)[0];
			$d[$key]->thumb  = $pics[$value->thumb];
		}

		$count = MemberSubsService::getList($raw, TRUE);

		$assign = [
			'total' => $count,
			'list' => $d,
		];

		return $this->echoJson($assign);
	}

	/**
	 * [detailAction 详情]
	 * @return [type] [description]
	 */
	public function detailAction()
	{
		$this->getUser();
		$id = $this->dispatcher->getParam('id');
		$data = MemberSubsService::getInfo($id);
		if(!$data)
			return $this->echoJson($data);

		$data->dics_ids = explode(",",$data->dics_ids);

		$arr = new \stdClass();
		$arr->name = "subs_hd_pic";
		$pics = ConfigsService::getVal($arr);
		$pics = json_decode(json_encode($pics),TRUE);

		$list = HouseService::getSubs($data);
		// var_dump($this->sql());die;
		$d = new \stdClass();
		$d->thumb = $pics[$data->cate_id];

		foreach ($data as $key => $value) {
			$d->$key = $value;
			unset($d->dics_ids);
			unset($d->p);
			unset($d->page_num);	
		}
		$li = [];
		foreach ($list as $key => $value) {
			$li[$key] = $value;
			$li[$key]->go_url = $this->config->gourl . $this->config->urls->house . $value->id;
		}

		$assign = [
			"info" => $d,
			"list" => $li
		];

		return $this->echoJson($assign);
	}

	/**
	 * [doAction 保存]
	 * @return [type] [description]
	 */
	public function doAction()
	{
		$this->getUser();
		$raw = $this->validation(new MemberSubsValidation, $this->request->getJsonRawBody());
		if(count($raw->dics_ids) <=0)
			return $this->echoJson('dics_ids is required');
		$data = MemberSubsService::setSubs($raw);
		return $this->echoJson('ok');
	}

	/**
	 * [createAction 更新]
	 * @return [type] [description]
	 */
	public function createAction()
	{
		$this->getUser();
		$raw = $this->validation(new MemberSubsValidation, $this->request->getJsonRawBody());
		if(count($raw->dics_ids) <=0)
			return $this->echoJson('dics_ids is required');
		$data = MemberSubsService::setSubs($raw);
		return $this->echoJson('ok');
	}

	/**
	 * [updateAction 更新]
	 * @return [type] [description]
	 */
	public function updateAction()
	{
		$this->getUser();
		$raw = $this->validation(new MemberSubsValidation, $this->request->getJsonRawBody());
		if(count($raw->dics_ids) <=0)
			return $this->echoJson('dics_ids is required');
		$data = MemberSubsService::setSubs($raw);
		return $this->echoJson('ok');
	}

	/**
	 * [deleteAction 删除]
	 * @return [type] [description]
	 */
	public function deleteAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = MemberSubsService::forever($raw->id);
		return $this->echoJson('ok');
	}

}